class Transaction < ActiveRecord::Base
  STATES = {
    :WAITING => 1,
    :DENIED => 2,
    :APPROVED => 3,
    :FINISHED => 4,
    :EXPIRED => 5
  }

  REVERSE_STATES = {
    1 => :WAITING,
    2 => :DENIED,
    3 => :APPROVED,
    4 => :FINISHED,
    5 => :EXPIRED
  }

  ADMIN_PERMISSION = [STATES[:APPROVED], STATES[:FINISH], STATES[:DENIED]]
  USER_PERMISSION = [STATES[:WAITING]]
  REMINDER_ENABLE = [STATES[:APPROVED], STATES[:EXPIRED]]

  MAX_DAYS_FOR_RETURN = 14.days

  belongs_to :book
  belongs_to :user

  validates :book_id, :user_id, :status, presence: true

  attr_accessible :book, :user
  
  scope :every_thing, where("status IS NOT NULL")

  def self.by_user(user)
    if user.admin?
      every_thing
    else
      where("user_id =  ?", user.id)
    end
  end

  def self.by_status(status)
    if status.nil?
      every_thing
    else
      where("status = ?",  status) unless status.nil?
    end
  end
  
  def self.all_transactions(user, status = nil)
    Transaction.by_user(user).by_status(status)
  end
end
